import { HOME } from '../actionTypes';
import { message } from 'antd';
import fetch from '../utils/fetch';
import config from '../config';
import * as R from 'ramda';

export const update = (payload) => ({
  type: HOME,
  payload,
});

/**
 * 关闭弹框
 */
export const hideModal = () => update({ modal: false });

export const reject = () => () => message.info('放弃则无法使用本网站，请离开.');

/**
 * 构建侧边栏
 * @param cid
 */
export const updateSideCategory = (cid = 0) => (dispatch, getState) => {
  const { list } = getState().admin.category;
  let arr = [];
  let path = [];
  let firstLevel = false;
  let subMenuIds = [];
  for (let i = 0, len = list.length; i < len; i++) {
    if (list[i].id === +cid) {
      arr = [list[i]].concat(list[i].subMenu.sort((a, b) => a.rank - b.rank));
      path.push(list[i]);
      subMenuIds = R.pluck('id')(list[i].subMenu);
      firstLevel = true;
      break;
    }
    const subMenu = list[i].subMenu.sort((a, b) => a.rank - b.rank);
    for (let j = 0, len1 = subMenu.length; j < len1; j++) {
      if (subMenu[j].id === +cid) {
        arr = [list[i]].concat(subMenu);
        path = path.concat(list[i], subMenu[j]);
        break;
      }
    }
  }
  dispatch(update({ sideCategory: arr, categoryPath: path, sideArticle: [] }));
  return { subMenuIds, firstLevel };
};

/**
 * 获取侧边栏文章标题列表
 * @param cid
 */
export const getSideArticleList = (cid) => async (dispatch, getState) => {

  if (!cid) {
    return false;
  }

  const res = await fetch({
    method: 'GET',
    url: `${config.host}/api/article/category/${cid}`,
    param: {
      orderby: 'id',
    },
  }).catch(ex => console.error(ex));
  if (res.code === 200) {
    dispatch(update({ sideArticle: res.data.data }));
  } else {
    message.error('获取文章失败');
  }
};

/**
 * 通过cid获取栏目文章
 * @param cid
 */
export const getArticleByCategoryId = (cid, subMenuIds) => async (dispatch) => {
  if (!cid) {
    return false;
  }

  const res = await fetch({
    method: 'GET',
    url: `${config.host}/api/article/category/${cid}?subMenuIds=${subMenuIds.join(',')}`,
  }).catch(ex => console.error(ex));
  if (res.code === 200) {
    dispatch(update({ categoryArticles: res.data }));
    return res;
  } else {
    message.error('获取文章失败');
  }
};

/**
 * 获取协议信息
 */
export const getProtocol = () => async (dispatch, getState) => {
  const res = await fetch({
    method: 'GET',
    url: `${config.host}/api/system/protocol`,
  }).catch(ex => console.error(ex));
  if (res.code === 200) {
    dispatch(update({ protocol: res.data }));
  } else {
    message.error('网络服务异常');
  }
};

/**
 * 通过id获取文章
 * @param id
 */
export const getArticleById = (id) => async (dispatch) => {
  if (!id) {
    return false;
  }

  const res = await fetch({
    method: 'GET',
    url: `${config.host}/api/article/${id}`,
  }).catch(ex => console.error(ex));
  if (res.code === 200) {
    dispatch(update({
      article: res.data,
    }));
  } else {
    message.error('网络服务异常');
  }
};

/**
 * 通过uid获取用户投资信息
 */
export const getInfoByUid = (uid) => async (dispatch, getState) => {
  const { uid: uidInState = '' } = getState().home;
  const token = localStorage.getItem('token-1');
  if (!uid && !uidInState) {
    return false;
  }

  const res = await fetch({
    method: 'GET',
    url: `${config.host}/api/auth/info/${uid || uidInState}`,
    headers: {
      authorization: token,
    },
  }).catch(ex => console.error(ex));
  if (res.code === 200) {
    dispatch(update({
      my: res.data,
      uid,
    }));
  } else {
    message.error(res.msg || '没有此用户');
  }
};
